package cn.avalon.service.impl;

import cn.avalon.domain.Product;
import cn.avalon.domain.Store;
import cn.avalon.dto.ProductDto;
import cn.avalon.feign.StoreService;
import cn.avalon.mapper.ProductMapper;
import cn.avalon.service.ProductService;
import cn.avalon.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: Avalon
 * @date: 2022年09月19日 ： 21:28
 * @version: ver1.0
 * @email 709785494@qq.com
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private StoreService storeService;

    @Override
    public AjaxResult add(ProductDto productDto) {
        try {
            // 1.保存商品
            Product product = new Product();
            product.setName(productDto.getName());
            product.setPrice(productDto.getPrice());
            productMapper.add(product);

            // 2.保存库存
            Store store = new Store();
            store.setProduct_id(product.getId());
            store.setNumber(productDto.getNumber());
            AjaxResult add = storeService.add(store);
            if (add.isSuccess()) {
                return add;
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败，请重试！");
        }
    }
}
